<template>
    <view v-if="show" class="popup" style="background: rgba(0,0,0,0.3);position: fixed;top: 0;width: 100%;height: 100%;">
        <view class="popup-content" style="width: 320px;background: #fff;border-radius: 10px;">
            <view class="header">
                需要您的授权
            </view>
            <view style="text-align: center;">
                <view style="text-align: center;padding-top: 17px;">
                    <view>为了提供更好的服务</view>
                    <view>请允许获取您的用户信息</view>
                    <view>如暂不需要，可点击取消或拒绝</view>
                </view>
                <!-- <view style="text-align: center;">
                    <image src="../static/image/shouquan.png" mode="widthFix" style="width: 230px;"></image>
                </view> -->
                <view style="padding-bottom: 5px;" class="btns">

                    <button type="default" class="btn" @click="cancel()">取消</button>

                    <button type="default" class="btn auth" @click="authorize()">授权</button>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
export default {
    props: ['show'],
    name: "authorize",
    data () {
        return {
            ifAuthorize: true
        };
    },
    methods: {
        cancel () {
            // this.show = false
            this.$emit('update:show', false)
        },
        authorize () {
            this.setUserInfo().then(res => {

                console.log('能获取到不',res)

                this.$emit('update:show', false)
            });
        },
        setUserInfo(){

           return new Promise((resolve, reject) => {
                uni.getUserProfile({
                    desc: '用于完善个人资料' // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
                })
                    .then((response) => {
                        if (response[1].errMsg === 'getUserProfile:ok') {
                            uni.setStorageSync('userInfo', response[1].userInfo); //存储用户信息
                            resolve(response[1]);//返回的信息 
                        } else {
                            uni.showToast({
                                title: '授权失敗',
                                icon: 'error'
                            });
                        }
                    }).catch((reject) => {
                        console.log('拒绝授权', reject)
                    })
            })

        }
    }
}
</script>
<style lang="scss" scoped>
.popup {
    .popup-content {
        position: absolute;
        top: 40%;
        left: 50%;
        margin-left: -160px;
        transform: translateY(-50%);
    }
}
.header {
    padding: 8px 0;
    line-height: 33px;
    border-bottom: 0.5px solid #eee;
    text-align: center;
}
.btns {
    margin-top: 40px;
    display: flex;
    justify-content: space-around;
    .btn {
        border: none;
        outline: none;
        background-color: #eaeaea !important;
        color: #333;
        border-radius: 44px;
        width: 40%;
        margin-bottom: 20px;
        &.auth {
            background-color: #04be01 !important;
            color: #fff !important;
        }
    }
}
</style>